import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class TweetPanelView extends JPanel {

	private static Border paneEdge;
	private static Border lowerEtched;
	public static TweetPanelView tweetPanel = new TweetPanelView();
	public static ArrayList<TwitterUserModel> twitterUser;

	public static void intializeTweetPanel() {
		paneEdge = BorderFactory.createEmptyBorder(40, 10, 10, 10);
		setLowerEtched(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		tweetPanel.setOpaque(false);
		tweetPanel.setBorder(paneEdge);
		tweetPanel.setLayout(new BoxLayout(tweetPanel, BoxLayout.Y_AXIS));
		TweetPanelView.twitterUser = new ArrayList<TwitterUserModel>();
	}
	
	public void addCompForBorder(Border border,
			final TwitterUserModel twitterUser, Image profileImage,
			String tweet, Container container) {

		JPanel comp = new JPanel(new GridLayout(1, 1), false);
		comp.setPreferredSize(new Dimension((int) (Toolkit.getDefaultToolkit()
				.getScreenSize().width / 1.2), Toolkit.getDefaultToolkit()
				.getScreenSize().height / 10));
		comp.setMaximumSize(new Dimension((int) (Toolkit.getDefaultToolkit()
				.getScreenSize().width / 1.2), Toolkit.getDefaultToolkit()
				.getScreenSize().height / 10));

		ImageIcon imageIcon = new ImageIcon(profileImage);
		JLabel label = new JLabel(tweet, imageIcon, JLabel.LEFT);

		label.setCursor(new Cursor(Cursor.HAND_CURSOR));
		label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				Thread thread = new Thread() {
					@Override
					public void run() {
						try {
							if (twitterUser.link != null)
								Runtime.getRuntime()
										.exec("/bin/bash firefox "
												+ twitterUser.link).waitFor();
							else
								return;
						} catch (InterruptedException e) {
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				};
				thread.start();
			}
		});

		comp.add(label);
		comp.setBorder(border);
		comp.setBackground(Color.white);

		// Specify the index to be 0 so the JPanel will always be added to the
		// JScrollPane
		container.add(comp, 0);
	}

	public static String setTweet(TwitterUserModel tu, boolean hasLink) {
		String tweetColor;
		if (hasLink)
			tweetColor = "blue";
		else
			tweetColor = "gray";
		String tweet = "<html><pre><b>" + tu.twitterUserName + "</b>\t@"
				+ tu.twitterUserName + "\t<font color=\"gray\">"
				+ tu.originalTweetTime + "</font>\n" + "<font color=\""
				+ tweetColor + "\">" + tu.twitterUserStatuses
				+ "</font></pre></html>";
		return tweet;
	}

	public static void updateTweetPanel(
			ArrayList<TwitterUserModel> updatedTweets) {
		String tweet;
		for (TwitterUserModel tu : updatedTweets) {
			if (tu.link != null)
				tweet = TweetPanelView.setTweet(tu, true);
			else
				tweet = TweetPanelView.setTweet(tu, false);
			tweetPanel.addCompForBorder(tweetPanel.getLowerEtched(), tu,
					tu.profileImage, tweet, tweetPanel);
		}
	}

	public static void addTwitterUser(String twitterUserName) {
		TweetPanelView.twitterUser.add(new TwitterUserModel(twitterUserName));
	}

	public static Border getLowerEtched() {
		return lowerEtched;
	}

	public static void setLowerEtched(Border lowerEtched) {
		TweetPanelView.lowerEtched = lowerEtched;
	}

	public void update(ArrayList<TwitterUserModel> newTweets) {
		if (TweetPanelModel.newTweets.size() > 0) {
			TweetPanelView.updateTweetPanel(TweetPanelModel.newTweets);
			TwitterFrameView.tweetScrollPane.setViewportView(tweetPanel);

		}
	}
}
